package thread.sync.interference;
import static java.lang.System.out;

/*
 * Thread interference Non Sync version - result may vary in each run
 */
public class XYZ_NoSync implements Runnable{
	
	static int x = 0 , y = 0, z = 0;
	
	public static void main(String[] args)
					throws InterruptedException{
		
		new Thread(new XYZ_NoSync()).start();
		new Thread(new XYZ_NoSync()).start();
	}
	@Override
    public void run() {
		calculate();
	}
	
//...}
	
	static void calculate(){
		for (int i=0; i < 100; i++) {
			x++;
			delay();
			y = y + x;
			delay();
			z = z + y;
		}
		out.println("z = " + z);
	}
	
	static void delay(){
		for (double d = 0; d < 1000000; d++) {
			; // A way to delay without sleep,
			// sleep will die this thread for n seconds
		}
	}
}

// Non Synch version - result may vary in each run
/*
z = 1293800
z = 1373400

z = 1313346
z = 1373347

z = 1333200
z = 1373398
*/

